<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"
%><%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"
%><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"
%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
%>
<head>
    <title><fmt:message key="category" /></title>
	<c:url var="treeUrl" value="/domain/rest/category/children" scope="page"/>
	<c:url var="contentUrl" value="/domain/category/search" />

	
	<script language="JavaScript">
	<!--
		dojo.require("dijit.dijit"); //optimize: load dijit layer
		dojo.require("dijit.layout.SplitContainer");
		dojo.require("dijit.layout.ContentPane");
		dojo.require("dojox.data.JsonRestStore");
		dojo.require("dijit.tree.ForestStoreModel");
		dojo.require("dijit.Tree");

		function myTree() {
            var store = new dojox.data.JsonRestStore( {
				labelAttribute: 'name',
				idAttribute: 'inode',
                target: "${treeUrl}"});

            var model = new dijit.tree.ForestStoreModel( {
                store: store,
				deferItemLoadingUntilExpand: true,
				query: "root",
				mayHaveChildren: function(item){
					var had = (item === this.root);
					if(!had) {
						return dojo.some(['hasChildren', 'children'], function(attr){
							return this.store.hasAttribute(item, attr);
						}, this);
					}
					return had;
				}
			});

            return new dijit.Tree({
				model: model,
				showRoot: false,
				openOnClick: true,
                id: "MenuTree"});
        }

		dojo.addOnLoad(function() {
			dojo.parser.parse();
			var treePane = dijit.byId("treePane"); 
			var tree = myTree();
			//Add dbclick for node
			dojo.connect(tree, "onDblClick", function(storeItem, nodeWidget, event){
				var contentNode = dijit.byId("contentNode");
				var identity = this.model.getIdentity(storeItem);
				dojo.byId('currentSelectedNode').value = identity;
				if(storeItem.type && storeItem.type == 'host') {
					contentNode.attr("href", "${contentUrl}?decorator=none&category.siteInode=" + identity);
				} else {
					contentNode.attr("href", "${contentUrl}?decorator=none&category.parentInode=" + identity);
				}
			});
			tree.startup();
			treePane.attr('content', tree.domNode); 

			dojo.query(".createLink").forEach(function(element) {
				dojo.connect(element, 'onclick', function(event) {
					var value = dojo.byId('currentSelectedNode').value;
					var href = element.href;
					var itemNode = tree.getNodesByItem(value);
					if(itemNode && itemNode[0]) {
						if(itemNode[0].item.type && itemNode[0].item.type == 'host') {
							element.href = href + "?hostId=" + value;
						} else {//This is category
							element.href = href + "?category.parentInode=" + value;
						}
					}
					//console.warn(itemNode);
					
					//element.href = href + "?categoryId=" + value;
					//Spring.remoting.getLinkedResource(element.id, {decorator: "none",categoryId: value}, true);
					//dojo.stopEvent(event);
				})
			})
			//dojo.subscribe("NewFolderItemCreated", FolderNavigationModel, "onNewFolderItem"); 
		});
	//-->
	</script>
</head>
<body class="tundra">
    <h1><fmt:message key="category" /></h1>
    <div class="buttons">
       <c:url var="createUrl" value="/domain/category/createForm" />
       <a id="createUrl" class="createLink" href="${createUrl}"><span><fmt:message key="label.link.create" /><fmt:message key="category" /></span></a>
    </div>
    <c:url var="searchUrl" value="/domain/category/search" />
    <h2><fmt:message key="label.search.byExample" /></h2>
	<input type="hidden" id="currentSelectedNode" />
	<div dojoType="dijit.layout.SplitContainer"
		orientation="horizontal"
		sizerWidth="5"
		activeSizing="false"
		style="border: 2px solid black; float: left; width: 100%; height: 300px;">

		<div dojoType="dijit.layout.ContentPane" id="treePane" sizeMin="20" sizeShare="20">
			<div id="treeNode"></div>
		</div>

		<div dojoType="dijit.layout.ContentPane" id="contentNode" sizeMin="50" sizeShare="50" href="${contentUrl}?decorator=none" preLoad="true">
			
		</div>
	</div>

</body>
